package com.colorfruit.admin.business.schedule;

import com.colorfruit.admin.business.dao.ConfigCommonMapper;
import com.colorfruit.admin.business.dao.GlobalProcessionControlMapper;
import com.colorfruit.admin.pojo.constant.CommonConstant;
import com.colorfruit.admin.pojo.entity.ConfigCommon;
import com.colorfruit.admin.pojo.entity.GlobalProcessionControl;
import com.colorfruit.admin.pojo.enums.GlobalStatusEnum;
import com.colorfruit.framework.common.util.DataConvertUtil;
import com.colorfruit.framework.common.util.DataValidateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Slf4j
@Component
@Transactional
public class GlobalStatusUpdateSchedule {

    @Autowired
    ConfigCommonMapper configCommonMapper;

    @Autowired
    GlobalProcessionControlMapper globalProcessionControlMapper;


    @Scheduled(cron = "0 0/5 * * * ? ")
    public void GlobalStatusUpdate() {

        GlobalProcessionControl recentProcession = globalProcessionControlMapper.getRecentProcession();
        ConfigCommon startShopCommon = configCommonMapper.getConfigInfoByCode(CommonConstant.START_SHOP_TIME);
        Long fiveMinuteMillion = DataConvertUtil.getMillisTime(0, 5, 0);

        //开始下单触发点
        if (startShopCommon != null && DataValidateUtil.verifyString(startShopCommon.getConfigValue())) {

            long startShopTime = DataConvertUtil.StringToDateWithFormat(startShopCommon.getConfigValue(), CommonConstant.DATE_FORMAT).getTime();
            long nowTime = new Date().getTime();

            //如果当前时间在系统配置时间之后的五分中内, 并且前一个阶段是预采购阶段
            if (nowTime >= startShopTime
                    && nowTime - startShopTime <= fiveMinuteMillion
                    && recentProcession.getRecordStatus() == GlobalStatusEnum.PRE_PURCHASE.getCode()
            ) {
                log.warn("====================GlobalStatusUpdate_startShopTime={}===========================", recentProcession.toString());
                globalProcessionControlMapper.updateStatus(recentProcession.getId(), GlobalStatusEnum.SHOPPING.getCode());
            }
        }
        //结束下单触发点
        ConfigCommon stopShopCommon = configCommonMapper.getConfigInfoByCode(CommonConstant.END_SHOP_TIME);
        if (stopShopCommon != null
                && DataValidateUtil.verifyString(stopShopCommon.getConfigValue())) {

            long stopShopTime = DataConvertUtil.StringToDateWithFormat(stopShopCommon.getConfigValue(), CommonConstant.DATE_FORMAT).getTime();
            long nowTime = new Date().getTime();

            //如果当前时间在系统配置时间之后的五分中内, 并且前一个阶段是预采购阶段
            if (nowTime >= stopShopTime
                    && nowTime - stopShopTime <= fiveMinuteMillion
                    && recentProcession.getRecordStatus() == GlobalStatusEnum.SHOPPING.getCode()
            ) {
                log.warn("====================GlobalStatusUpdate_stopShopTime={}===========================", recentProcession.toString());
                globalProcessionControlMapper.updateStatus(recentProcession.getId(), GlobalStatusEnum.PURCHASE.getCode());
            }
        }
        //重新开始触发点
        ConfigCommon restartCommon = configCommonMapper.getConfigInfoByCode(CommonConstant.RESTART_GLOBAL_TIME);
        if (restartCommon != null
                && DataValidateUtil.verifyString(restartCommon.getConfigValue())) {

            long restartTime = DataConvertUtil.StringToDateWithFormat(restartCommon.getConfigValue(), CommonConstant.DATE_FORMAT).getTime();
            long nowTime = new Date().getTime();

            //如果当前时间在系统配置时间之后的五分中内, 并且前一个阶段是预采购阶段
            if (nowTime >= restartTime
                    && nowTime - restartTime <= fiveMinuteMillion
                    && recentProcession.getRecordStatus() == GlobalStatusEnum.TRANSPORT.getCode()
            ) {
                log.warn("====================GlobalStatusUpdate_restartTime={}===========================", recentProcession.toString());
                GlobalProcessionControl globalProcessionControl = new GlobalProcessionControl();
                globalProcessionControl.setRecordStatus(GlobalStatusEnum.PRE_SALE.getCode());
                globalProcessionControl.setCreateTime(new Date());
                globalProcessionControlMapper.insert(globalProcessionControl);
            }
        }

    }

}
